<?php
require "../includes/db.php"; 
$mysqli = new MossCMS();
if ($_SESSION[$prefix.'_username'] == ""){  header("Location: ../login.php"); exit;}
if ($_SESSION[$prefix."_security"] < 1){header("location:../login.php?action=5"); exit;}

$action = $_GET['action'];

//UPDATE MENU ORDER
if($action == "menu_update"){
	$order 	= $_GET['order'];
	$menu	= $_GET['menu'];
	$neworder = explode(",", $order);
		
			for($x=0; $x <= ($size+2); $x++){
			$current = $neworder[$x];
				$mysqli->menu_link_order($menu,$current,$x+1);
			}
}//END UPDATE

//DELETE LINK
if($action == "delete_link"){
	$menu	= $_GET['link_id'];
		$mysqli->menu_link_remove($menu);
}//END UPDATE

if($action == 'publish'){
	include "includes/menu_generator.php";
	header("Location:?message=published");
}


if ($_SERVER['REQUEST_METHOD'] == "POST") { 

$menu_id	= $_POST['menu_id'];
$menu_name	= $_POST['name'];
$active		= $_POST['active'];

$link_title	= $_POST['link_title'];
$link_url	= $_POST['link_url'];

$submit		= $_POST['submit'];

if($submit == "Update"){
	$mysqli->menu_edit($menu_id,$menu_name,$active);
}


if($submit == "Add Link"){
	$mysqli->menu_link_add($menu_id,$link_title,$link_url);
}

}//END POST
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../includes/admin.css"/>
<title><?php echo $global_title;?></title>


<?php echo $jquery_global;?>
<?php echo $jquery_ui_global;?>
<?php echo $jgrowl_global;?>

<script type="text/javascript">	
	function updateOrder(currentItem)
    {
    //alert(currentItem);
    
		var result = $('#sortable-menu'+currentItem).sortable('toArray');
		
	for(var i=0;i<result.length;i++){
	if(i == 0){var newOrder = result[i];}else{
	var newOrder = newOrder + "," + result[i];}
	}
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
	$.jGrowl("The Menu Has Been Saved!", {header: 'Updated!', position: 'top-left'});
			
		}
	}
	
	var queryString = "?action=menu_update&order=" + newOrder + "&menu=" + currentItem;
	ajaxRequest.open("GET", "menu.php" + queryString, true);
	ajaxRequest.send(null); 
	//alert(queryString);
	}
</script>


<style type="text/css">	
	<?php $results = $mysqli->menu_list();
      	for($x=0; $x < sizeof($results); $x++) {

			$current_menu_id 	= $results[$x]["menu_id"];
			$current_menu_name	= $results[$x]["menu_name"];
			$current_menu_active= $results[$x]["menu_active"];
				if($x > 0){echo ", ";}
				echo "#sortable-menu".$current_menu_id;
		} ?>
	
	  { list-style-type: none; margin: 0; padding: 0; float: left; margin-right: 10px; background: #eee; padding: 5px; width: 143px;}
	  

	<?php $results = $mysqli->menu_list();
      	for($x=0; $x < sizeof($results); $x++) {

			$current_menu_id 	= $results[$x]["menu_id"];
			$current_menu_name	= $results[$x]["menu_name"];
			$current_menu_active= $results[$x]["menu_active"];
				if($x > 0){echo ", ";}
				echo "#sortable-menu".$current_menu_id. " li ";
		} ?>
		
		{ margin: 2px; padding: 2px; font-size: 6pt; width: 120px; }
	</style>

	<script type="text/javascript">
	$(function() {
	$("<?php $results = $mysqli->menu_list();
      	for($x=0; $x < sizeof($results); $x++) {

			$current_menu_id 	= $results[$x]["menu_id"];
			$current_menu_name	= $results[$x]["menu_name"];
			$current_menu_active= $results[$x]["menu_active"];
				if($x > 0){echo ", ";}
				echo "#sortable-menu".$current_menu_id;
		} ?>").sortable({
			connectWith: '.connectedSortable',
			/*receive: function(event, ui) {var element = ui.receiver; alert(element); updateOrder(element); $.jGrowl("The Banner Has Been Added to the Set", {header: 'Updated!', position: 'top-left'});} */
			
		}).disableSelection();
	});
	</script>






</head>
<body>

<div id="wrap">
	<div id="page-header">
		<?php include('includes/header.php');?>

	</div>
	
	<div id="page-body">
		<div id="tabs">
			<?php $tab="main"; include('includes/header_tabs.php');?>
		</div>

		<div id="acp">
		<div class="panel">
			<span class="corners-top"><span></span></span>
				<div id="content">
				<div id="menu">
						
					<?php include('includes/menu_top.php');?>	
					<?php include('includes/menu_main.php');?>
					</div>
	
					<div id="main">
<a name="maincontent"></a>


     
    <h2>Current Menu</h2>	
    <p><a href="?action=publish">Publish Menu</a> </p>
<?php
	$results = $mysqli->menu_list();
      	for($x=0; $x < sizeof($results); $x++) {

			$current_menu_id 	= $results[$x]["menu_id"];
			$current_menu_name	= $results[$x]["menu_name"];
			$current_menu_active= $results[$x]["menu_active"];
			
			?>
			<fieldset><legend><?php echo $current_menu_name;?></legend>
				<a name="<?php echo $current_menu_id;?>"></a>
				<form action="" method="post" name="<?php echo $current_menu_id;?>" id="<?php echo $current_menu_id;?>" >
					<input type="hidden"  name="menu_id" value="<?php echo $current_menu_id;?>">
					<strong><a href="#<?php echo $current_menu_id;?>" onclick="updateOrder('<?php echo $current_menu_id;?>');">Save Menu</a></strong><br />
			<ul id="sortable-menu<?php echo $current_menu_id;?>" class="connectedSortable" onchange="updateOrder('#sortable-menu<?php echo $current_menu_id;?>');">
				<?php
					$menu_links = $mysqli->menu_link_list($current_menu_id);
      	for($y=0; $y < sizeof($menu_links); $y++) {

						$menu_id 	= $menu_links[$y]["menulink_id"];
						$menu_name	= $menu_links[$y]["menulink_link_name"];
						$delete_link = "<a href=\"?action=delete_link&link_id=$menu_id\">Delete Link</a>";
						?>
						<li class="ui-state-default" id="<?php echo $menu_id;?>"><?php echo $menu_name;?> - <?php echo $delete_link;?></li>
						<?php }
					?>
				
			
			</ul>
			
			
			<div style="margin-left: 200px;">			
			
			<dl>
				<dt>Sub-Menu Name:</dt>
				<dd><input id="name" name="name" value="<?php echo $current_menu_name;?>"></dd>
			</dl>
			
			<dl>
				<dt>Sub-Menu Active:</dt>
				<dd><select name="active" id="active" >
						<option value="1" <?php if($current_menu_active == 1){echo "selected";}?> >Yes</option>
						<option value="0" <?php if($current_menu_active == 0){echo "selected";}?> >No</option>
					  </select>
				</dd>
			</dl>	
			
			<p class="submit-buttons"><input type="submit" class="button1" name="submit" id="submit" value="Update" /></p>
				 
				
					  <div class="clear"></div>
			  
					  
		</div>
	
		<fieldset>
			<legend>Add Link</legend>
		
			<dl>
				<dt>Link Title:</dt>
				<dd><input id="link_title" name="link_title" size="50" ></dd>
			</dl>
			<dl>
				<dt>Link URL:</dt>
				<dd><input id="link_url" name="link_url" size="50" ></dd>
			</dl>
<p class="submit-buttons"><input type="submit" class="button1" name="submit" id="submit" value="Add Link" /></p>
		
		</fieldset>		
		
		
	</form>
</fieldset>

<?php } ?>


    
		</div><!-- main -->
	</div><!-- content -->
<span class="corners-bottom"><span></span></span>
</div><!-- panel -->
</div><!-- acp -->

	
	<div id="page-footer">
	<?php include('includes/footer.php');?>
				</div>
</div><!-- page-body -->
</div><!-- wrap -->

</body> 
</html>